package com.softer.wxzj.common.util;

import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.entity.BaseModifyDetail;
import com.softer.wxzj.entity.BaseOwnerInfo;
import com.softer.wxzj.entity.base.FieldMeta;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取两个相同对象不同的数据
 *
 * @author Gong Zhiyang
 * @date 2019/4/29
 */
public class GetDifferenceUtil {
       /**
     * 获取两个相同对象不同的数据
     *
     * @param oldObject 旧值
     * @param newObject 新值
     * @return 返回不同数据集合
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static List<BaseModifyDetail> getDifference(Object oldObject, Object newObject)
            throws IllegalArgumentException, IllegalAccessException {
        List<BaseModifyDetail> list = new ArrayList<>();
        Field[] beforeFields = oldObject.getClass().getDeclaredFields();
        Field[] afterFields = newObject.getClass().getDeclaredFields();
        for (int i = 0; i < beforeFields.length; i++) {
            Field beforeField = beforeFields[i];
            Field afterField = afterFields[i];
            beforeField.setAccessible(true);
            afterField.setAccessible(true);
            Object before = beforeField.get(oldObject);
            Object after = afterField.get(newObject);
            if (beforeField.get(oldObject) != null && afterField.get(newObject) != null) {
                if (!beforeField.get(oldObject).equals(afterField.get(newObject))) {
                    setValue(list, beforeField.getName(), beforeField.get(oldObject), afterField.get(newObject), beforeField);
                }
                //oldObject值为null，newObject值为""不做处理（后加条件：!"".equals(afterField.get(newObject))）  6.14号更新
            }else if (beforeField.get(oldObject) == null && afterField.get(newObject) != null && !"".equals(afterField.get(newObject))) {
                setValue(list, beforeField.getName(), "", afterField.get(newObject), beforeField);
                //7.1注释  前端实体为null不做处理   （前端传空字段为""，非null，只对""做操作，可以避免变更纯后台操作的字段）
            }/*else if (beforeField.get(oldObject) != null && afterField.get(newObject) == null) {
                setValue(list, beforeField.getName(), beforeField.get(oldObject), "", beforeField);
            }*/
        }
        return list;
    }
    /**
     * 获取两个相同对象不同的数据  注意：仅可获取当前实体字段，不能获取父级实体字段
     * @param oldObject 旧值
     * @param newObject 新值
     * @return 返回不同数据集合
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public List<BaseModifyDetail> getOnlyDifference(Object oldObject, Object newObject, BaseOwnerInfo baseOwnerInfo)
            throws IllegalArgumentException, IllegalAccessException {
        List<BaseModifyDetail> list = new ArrayList<>();
        Field[] beforeFields = oldObject.getClass().getDeclaredFields();
        Field[] afterFields = newObject.getClass().getDeclaredFields();
        for (int i = 0; i < beforeFields.length; i++) {
            Field beforeField = beforeFields[i];
            Field afterField = afterFields[i];
            beforeField.setAccessible(true);
            afterField.setAccessible(true);
            if (beforeField.get(oldObject) != null && afterField.get(newObject) != null) {
                if (!beforeField.get(oldObject).equals(afterField.get(newObject))) {
                    setOnlyValue(list, beforeField.getName(), beforeField.get(oldObject), afterField.get(newObject), beforeField,baseOwnerInfo.getName(),baseOwnerInfo);
                }
                //oldObject值为null，newObject值为""不做处理（后加条件：!"".equals(afterField.get(newObject))）  6.14号更新
            }else if (beforeField.get(oldObject) == null && afterField.get(newObject) != null && !"".equals(afterField.get(newObject))) {
                setOnlyValue(list, beforeField.getName(), "", afterField.get(newObject), beforeField,baseOwnerInfo.getName(),baseOwnerInfo);
                //  7.1号注释掉以下代码    新值为null则不做操作，（前端清空数据传""，非前端传参则为null,所以不对null做比较） zcz
            }else if (beforeField.get(oldObject) != null && afterField.get(newObject) == null ) {
                setOnlyValue(list, beforeField.getName(), beforeField.get(oldObject), "", beforeField,baseOwnerInfo.getName(),baseOwnerInfo);
            }
        }
        return list;
    }

    /**
     * 设置不同的参数类型，添加到集合
     *
     * @param list       不同类型结合
     * @param columnName 列名
     * @param oldValue   旧值
     * @param newValue   新值
     */
    private  void setOnlyValue(List<BaseModifyDetail> list, String columnName, Object oldValue, Object newValue, Field field,String name,BaseOwnerInfo baseOwnerInfo) {
        //获取变更列名的中文名
        FieldMeta annotation = field.getAnnotation(FieldMeta.class);
        if (annotation!=null) {
            BaseModifyDetail baseModifyDetail = new BaseModifyDetail();
            baseModifyDetail.setColumnName(columnName);
            baseModifyDetail.setOldValue(String.valueOf(oldValue));
            baseModifyDetail.setNewValue(String.valueOf(newValue));
            if ((NumStrEnum.ZERO.getNum()).equals(baseOwnerInfo.getInfoType())) {
                baseModifyDetail.setName(name + annotation.name());
            } else {
                baseModifyDetail.setName("共有人--" + name + annotation.name());
            }
            if ((NumStrEnum.ONE.getNum()).equals(baseOwnerInfo.getIsAffiliated())) {
                baseModifyDetail.setName(name + annotation.name() + "(" + baseOwnerInfo.getRoomName() + ")");
            }
            list.add(baseModifyDetail);
        }
    }
    private static void setValue(List<BaseModifyDetail> list, String columnName, Object oldValue, Object newValue, Field field) {
        //获取变更列名的中文名
        FieldMeta annotation = field.getAnnotation(FieldMeta.class);
        if(annotation!=null) {
            BaseModifyDetail baseModifyDetail = new BaseModifyDetail();
            baseModifyDetail.setColumnName(columnName);
            baseModifyDetail.setOldValue(String.valueOf(oldValue));
            baseModifyDetail.setNewValue(String.valueOf(newValue));
            baseModifyDetail.setName(annotation.name());
            list.add(baseModifyDetail);
        }
    }
}
